#include "UnionFind.h"
#include <numeric>
UnionFind::UnionFind(int totalNodes) {

    parents = std::vector<int>(totalNodes);
    std::iota(parents.begin(), parents.end(), 0);
}
void UnionFind::Union(int node1, int node2) {
    int root1 = find(node1);
    int root2 = find(node2);
    if (root1 != root2) {
        parents[root2] = root1;
    }
}
bool UnionFind::isConnected(int node1, int node2) {
    return find(node1) == find(node2);
}
int UnionFind::find(int node) {
    while (parents[node] != node) {
        parents[node] = parents[parents[node]];
        node = parents[node];
    }

    return node;
}
